<?php
class m_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function check_user_login($username, $password){
    	$db = $this->load->database('default', TRUE);
    	$array = array(
    		'username' => $username,
    		'password' => $password
    	);
    	$query = $db->get_where('user', $array);
    	$result = $query->num_rows();

    	return $result;
    }
    
    function insert_news($title, $shortDesc, $longDesc, $tag, $status, $imageThumb){
        $db = $this->load->database('default', TRUE);
        $this->insert_new_tag($tag);
        $array = array(
            'news_title' => $title,
            'short_desc' => $shortDesc,
            'long_desc' => $longDesc,
            'news_tag' => $tag,
            'status' => $status,
            'count_view' => 0,
            'image_thumb' => $imageThumb
        );
        $db->set('publish_date', 'NOW()', FALSE);
        $result = $db->insert('news', $array);
        return $result;
    }
    
    function update_news($title, $shortDesc, $longDesc, $tag, $status, $imageThumb, $news_id){
        $db = $this->load->database('default', TRUE);
        $this->insert_new_tag($tag);
        $array = array(
            'news_title' => $title,
            'short_desc' => $shortDesc,
            'long_desc' => $longDesc,
            'news_tag' => $tag,
            'status' => $status,
            'image_thumb' => $imageThumb
        );
        $db->where('news_id', $news_id);
        $result = $db->update('news', $array);
        return $result;
    }
    
    function delete_news($newsId){
        $db = $this->load->database('default', TRUE);
        $result = $db->delete('news', array('news_id' => $newsId));
        return $result;
    }
    
    function get_list_news($start, $limit, $status){
        $db = $this->load->database('default', TRUE);
        
        if($status != -1){
            $arrParam = array(
                "status" => $status
            );
            $query = $db->get_where("news", $arrParam);
        }
        else{
            $query = $db->get("news");
        }
        $result = $query->result();
        $count = count($result);

        $db->order_by("news_id", "desc");
        if($status != -1){
            $arrParam = array(
                "status" => $status
            );
            $query = $db->get_where("news", $arrParam, $limit, $start);
        }
        else{
            $query = $db->get("news", $limit, $start);
        }
        $result = $query->result();

        return array($count, $result);
    }

    function getNewsById($newsId){
        $db = $this->load->database('default', TRUE);
        $arrParam = array(
            "news_id" => $newsId
        );
        $query = $db->get_where("news", $arrParam);
        $result = $query->result();

        return $result;
    }
    
    function getBestNewsRead(){
        $db = $this->load->database('default', TRUE);
        $db->order_by("count_view", "desc");
        $query = $db->get("news", 5);
        $result = $query->result();

        return $result;
    }
    
    function getRelateNews($newsId){
        $db = $this->load->database('default', TRUE);
        $strSql = "SELECT * FROM news where news_id < " . $newsId . " order by news_id desc limit 3";
        $query = $db->query($strSql);
        $result = $query->result();

        return $result;
    }

    function countViewByNewsId($newsId, $countView){
        $db = $this->load->database('default', TRUE);
        $array = array(
            'count_view' => $countView
        );
        $db->where('news_id', $newsId);
        $result = $db->update('news', $array);
        return $result;
    }
    
    function getListVideoByPage($curPage, $pageSize){
        $db = $this->load->database('default', TRUE);
        $db->order_by("id", "desc");
        $query = $db->get("video", $pageSize, $curPage);
        $result = $query->result();
        return $result;
    }
    
    function getBestViewVideo(){
        $db = $this->load->database('default', TRUE);
        $db->order_by("count_view", "desc");
        $query = $db->get("video", 5);
        $result = $query->result();

        return $result;
    }
    
    function insertVideo($title, $imageLink, $videoLink){
        $db = $this->load->database('default', TRUE);
        $array = array(
            'title' => $title,
            'small_image' => $imageLink,
            'link' => $videoLink
        );
        $db->set('created_date', 'NOW()', FALSE);
        $result = $db->insert('video', $array);
        return $result;
    }

    function delete_video($videoId){
        $db = $this->load->database('default', TRUE);
        $result = $db->delete('video', array('id' => $videoId));
        return $result;
    }
    
    function insert_email($address, $subject){
        $db = $this->load->database('default', TRUE);
        $array = array(
            'email_address' => $address,
            'subject' => $subject
        );
        $result = $db->insert('email', $array);
        return $result;
    }
    
    function check_tag_exists($tag){
        $db = $this->load->database('default', TRUE);
        $array = array(
            "name" => $tag
        );
        $query = $db->get_where("tag", $array);
        $result = $query->result();
        return count($result);
    }
    
    function insert_new_tag($sourceTag){
        $db = $this->load->database('default', TRUE);
        $arrTag = explode(",", $sourceTag);
        foreach ($arrTag as $value) {
            $value = trim($value);
            $check = $this->check_tag_exists($value);
            if($this->check_tag_exists($value) == 0){
                $array = array(
                    "name" => $value
                );
                $db->insert('tag', $array);
            }
        }
    }
    
    function get_tag_list($limit){
        $db = $this->load->database('default', TRUE);
        if($limit > 0){
            $query = $db->get("tag", $limit, 0);
        }
        else{
            $query = $db->get("tag");
        }
        $result = $query->result();
        return $result;
    }
    
    function get_news_by_tag($start, $limit, $tag){
        $db = $this->load->database('default', TRUE);
        
        $db->like('news_tag', $tag);
        $db->where('status', 1);
        $query1 = $db->get("news");        
        $result1 = $query1->result();
        $count = count($result1);
        
        $db->like('news_tag', $tag);
        $db->where('status', 1);
        $db->order_by("news_id", "desc");
        $query2 = $db->get("news", $limit, $start);
        $result2 = $query2->result();

        return array($count, $result2);
    }
    
}
?>